package com.caifu.excel;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.caifu.service.YlMeicineDistributionService;
import com.caifu.util.ApplicationContextUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @author tww
 * ClassName: GrantHandler.java、
 * createTime: 2020年11月19日 14:35:28
 **/
@Data
@AllArgsConstructor
public class GrantHandler implements SheetWriteHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(GrantHandler.class);

    private String userNo;

    private String yanglaoyuan;

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        LOGGER.info("第{}个Sheet写入成功。", writeSheetHolder.getSheetNo());
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);
        //设置标题
        Row row2 = sheet.createRow(1);
        row2.setHeight((short) 800);
        Cell cell1 = row2.createCell(0);
        cell1.setCellValue(yanglaoyuan+"自备医疗药品发放表");
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeight((short) 400);
        cellStyle.setFont(font);
        cell1.setCellStyle(cellStyle);
        sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 1, 0, 8));
        //设置填表日期,填报人,联系方式
        Row row3 = sheet.createRow(2);
        row3.setHeight((short) 500);

        //获取容器中的bean
        YlMeicineDistributionService context = (YlMeicineDistributionService) ApplicationContextUtils.getContext("ylMeicineDistributionService");
        //查询老人信息
        Map<String, Object> userInfo = context.getUserInfo(userNo);
        Cell cell = row3.createCell(0);
        cell.setCellValue("姓名：" + userInfo.get("userName") + " " + "性别：" + userInfo.get("userSex")
                + " " + "部门（区）：" + userInfo.get("buildingName") + " " + "房号：" + userInfo.get("roomName") + " " + "床号：" + userInfo.get("bedName"));

        CellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle1.setAlignment(HorizontalAlignment.LEFT);
        sheet.addMergedRegionUnsafe(new CellRangeAddress(2, 2, 0, 8));


    }
}
